﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using BLL;
using Newtonsoft.Json.Linq;

namespace GoShopping.tools
{
    /// <summary>
    /// Likeds 的摘要说明
    /// </summary>
    public class Likeds : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string t = context.Request["t"].ToString();
            //0:根据UserID和GoodsID查询是否已经收藏
            //1:根据UserID和GoodsID删除
            //2:根据LikeID进行删除
            //3:添加收藏
            //4:获取收藏商品的详细信息
            switch (t)
            {
                case "0":
                    getAllBytwoId(context);
                    break;
                case "1":
                    deleteAllBytwoId(context);
                    break;
                case "2":
                    deleteById(context);
                    break;
                case "3":
                    add(context);
                    break;
                case "4":
                    getAll(context);
                    break;
            }
        }
        //获取收藏商品的详细信息
        private void getAll(HttpContext context)
        {
            int userId = int.Parse(context.Request["UserID"]);
            //调用方法获取数据
            List<Like_Goods> list= LikeBLL.getAllBLL(userId);
            //把数据转换为JSON格式
            JObject obj = new JObject();
            JArray array = JArray.FromObject(list);
            obj.Add("res", array);
            if (list.Count > 0)
            {
                context.Response.Write(obj);
            }
            else
            {
                context.Response.Write("{\"status\":\"获取失败！\"}");
            }
        }

        //添加收藏
        private void add(HttpContext context)
        {
            int goodsId = int.Parse(context.Request["GoodsID"]);
            int userId = int.Parse(context.Request["UserID"]);
            bool res = LikeBLL.addBLL(userId, goodsId);
            if (res)
            {
                context.Response.Write("{\"status\":\"已收藏！\"}");
            }
            else
            {
                context.Response.Write("{\"status\":\"未收藏！\"}");
            }
        }

        //根据LikeID进行删除
        private void deleteById(HttpContext context)
        {
            int LikeID= int.Parse(context.Request["LikeID"]);
            bool res=LikeBLL.deleteByIDBLL(LikeID);
            if (res)
            {
                context.Response.Write("{\"status\":\"已删除！\"}");
            }
            else
            {
                context.Response.Write("{\"status\":\"未删除！\"}");
            }
        }

        //根据UserID和GoodsID删除
        private void deleteAllBytwoId(HttpContext context)
        {
            int goodsId = int.Parse(context.Request["GoodsID"]);
            int userId = int.Parse(context.Request["UserID"]);
            bool res=LikeBLL.deleteNumBLL(userId,goodsId);
            if (res)
            {
                context.Response.Write("{\"status\":\"已删除！\"}");
            }
            else
            {
                context.Response.Write("{\"status\":\"未删除！\"}");
            }
        }

        //根据UserID和GoodsID查询
        private void getAllBytwoId(HttpContext context)
        {
            int goodsId = int.Parse(context.Request["GoodsID"]);
            int userId = int.Parse(context.Request["UserID"]);
            bool res=LikeBLL.selectNumBLL(userId, goodsId);
            if (res)
            {
                context.Response.Write("{\"status\":\"已收藏！\"}");
            }
            else
            {
                context.Response.Write("{\"status\":\"未收藏！\"}");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}